package com.zsinfo.guoss.supplier.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.netcenter.Event.Response;
import com.example.netcenter.RetrofitBuilder;
import com.example.netcenter.Url.ConstantsCode;
import com.example.netcenter.bean.ErrorBean;
import com.google.gson.Gson;
import com.zsinfo.guoss.supplier.MyApplication;
import com.zsinfo.guoss.supplier.R;
import com.zsinfo.guoss.supplier.base.BaseActivity;
import com.zsinfo.guoss.supplier.utils.Md5util;
import com.zsinfo.guoss.supplier.utils.SharedPreferencesUtil;

public class ChangePasswordActivity extends BaseActivity {
    EditText counter;
    EditText oldPassWord;
    EditText newPassWord;
    EditText confirmPassword;
    Button save;


    @Override
    protected int getContentLayoutRes() {
        return R.layout.activity_change_password;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        counter = null;
        oldPassWord = null;
        newPassWord = null;
        confirmPassword = null;
        save = null;
    }

    @Override
    protected void initView() {

        setTitle("修改登录密码");
        setBackAndLeftTitle("").setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        oldPassWord = (EditText) findViewById(R.id.oldPassWord);
        newPassWord = (EditText) findViewById(R.id.newPassWord);
        confirmPassword = (EditText) findViewById(R.id.confirmPassword);
        save = (Button) findViewById(R.id.save);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String oldPass = oldPassWord.getText().toString().trim();
                String newPass = newPassWord.getText().toString().trim();
                String confirmPass = confirmPassword.getText().toString().trim();
                if (TextUtils.isEmpty(oldPass)){
                    showToast("请输入原始密码");
                    return;
                }
                if (TextUtils.isEmpty(newPass) || TextUtils.isEmpty(confirmPass)){
                    showToast("请输入新密码");
                    return;
                }
                if (!newPass.equals(confirmPass)){
                    showToast("两次输入的新密码不一致");
                    return;
                }
                updatePwd(oldPass, newPass, confirmPass);
            }
        });

    }

    @Override
    protected void initNetData() {

    }

    @Override
    protected void customDestroy() {

    }

    /**
     * 修改登录密码
     * @param oldPass
     * @param newPass
     * @param confirmPass
     */
    private void updatePwd(String oldPass, String newPass, String confirmPass){
        GetNetCenter().GetNetCenter(this,
                RetrofitBuilder.CreateApi().UPDATEPWD(
                        ConstantsCode.update_pass,
                        SharedPreferencesUtil.getID(),
                        Md5util.GetMD5Code(oldPass),
                        Md5util.GetMD5Code(newPass),
                        Md5util.GetMD5Code(confirmPass)), new Response() {
                    @Override
                    public void OK(Object data) {
                        Log.e("lixl", data.toString());
                        showToast("修改成功");
                        SharedPreferencesUtil.clearData();
                        startActivity(new Intent(ChangePasswordActivity.this, LoginPwdActivity.class));
                        MyApplication.removeAll();
                    }

                    @Override
                    public void Fail(Throwable e) {
                        Log.e("lixl", e.getMessage());
                        if (e.getMessage().contains("Code")) {
                            ErrorBean errorBean = new Gson().fromJson(e.getMessage(), ErrorBean.class);
                            showToast(errorBean.getMsg());
                        } else {
                            showToast(e.getMessage());
                        }
                    }

                    @Override
                    public void complete() {

                    }
                });
    }
}
